﻿import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQml 2.15
import Qt.labs.platform 1.1
import FluentUI 1.0
import App 1.0
FluScrollablePage{
    id :root
    launchMode:FluPageType.SingleInstance
    FolderDialog {
        id: dialog
        title: qsTr("保存文件")
        visible: false
        onAccepted: {
            var file = dialog.currentFolder.toString().replace("file:///", "")
            var res = ControllerManager.towerDevice.exportXlsxFile(0x0001,file+'/grounded.xlsx')
            if(res === true)
            {
                showSuccess(qsTr('成功导出文件: ' +file+'/grounded.xlsx' ))
            }
            else {
                showError(qsTr('导出文件失败'))
            }
            dialog.close()
        }
    }
    RowLayout{
        spacing:15
        FluFrame {
            id:settings_frame
            Layout.topMargin: 20
            Layout.preferredHeight: root.height/1.2
            Layout.preferredWidth: root.width/2.5
            ColumnLayout{
                Layout.alignment: Qt.AlignHCenter
                FluText{
                   padding: 15
                   text:qsTr("接地极设备配置")
                   font: FluTextStyle.BodyStrong
                }
                Row{
                    FluText{
                       leftPadding: 30
                       id:settings_text
                       padding: 15
                       text:qsTr("地址个数")
                    }
                    FluSpinBox{
                       id: address_box
                       from: 0
                       to: 4
                       anchors.verticalCenter: settings_text.verticalCenter
                       editable:true
                    }
                }
                Row{
                    FluText{
                       leftPadding: 30
                       id:id_settings
                       padding: 15
                       text:qsTr("设备编号")
                    }
                    FluTextBox{
                       id:id_settings_box
                       width:parent.width/1.5
                       maximumLength: 4
                       validator: RegExpValidator { regExp: /^[a-zA-Z0-9]*$/ }
                       disabled:address_box.value === 0
                       anchors.verticalCenter: id_settings.verticalCenter
                    }
                }
                Row{
                    FluText{
                       leftPadding: 30
                       id:factor
                       padding: 15
                       text:qsTr("设备系数")
                    }
                    FluTextBox{
                       id:factor_box
                       width:parent.width/1.5
                       text: "1.0"
                       validator: DoubleValidator {}
                       disabled:address_box.value === 0
                       anchors.verticalCenter: factor.verticalCenter
                    }
                }
                Row {
                    FluText{
                       leftPadding: 30
                       id:grounded_name
                       padding: 15
                       text:qsTr("位置信息")
                    }
                    FluTextBox{
                       id:grounded_name_box
                       width:parent.width/1.5
                       disabled:address_box.value === 0
                       anchors.verticalCenter:grounded_name.verticalCenter
                    }
                }
                RowLayout{
                    Layout.topMargin: 10
                    Layout.leftMargin: 80
                    spacing:40
                    FluFilledButton{
                        Layout.alignment: Qt.AlignCenter
                        text:qsTr("生成配置")
                        disabled:address_box.value === 0
                        Layout.preferredWidth: 80
                        onClicked: {
                            var res = ControllerManager.towerDevice.createSettingsFile(
                                        0x0001,
                                        address_box.value,
                                        id_settings_box.text,
                                        factor_box.text,
                                        grounded_name_box.text
                            )
                            if(res){
                                showSuccess(qsTr("生成配置成功"))
                            }
                            else{
                                showWarning(qsTr("生成配置失败"))
                            }
                        }
                    }
                    FluFilledButton{
                        Layout.alignment: Qt.AlignCenter
                        text:qsTr("查看配置")
                        Layout.preferredWidth: 80
                        onClicked: {
                            if(ControllerManager.towerDevice.isfileExist(0x0001)){
                               ControllerManager.towerDevice.openSettingsFile(0x0001)
                            }
                            else{
                                showWarning(qsTr("不存在配置文件,请先生成配置"))
                            }
                        }
                    }
                }
                FluDivider{
                    Layout.preferredWidth: settings_frame.width
                    size:2
                    Layout.topMargin: 10
                    Layout.bottomMargin: 5
                }
                FluText{
                   padding: 15
                   text:qsTr("操作")
                   font: FluTextStyle.BodyStrong
                }
                RowLayout{
                    Layout.topMargin: 5
                    Layout.leftMargin: 20
                    spacing: 30

                    FluFilledButton{
                        id:set_button
                        Layout.alignment: Qt.AlignCenter
                        text:enabled === true? qsTr("设置"): qsTr("设置参数中")
                        Layout.preferredWidth: 80
                        onClicked: {
                            if(!ControllerManager.towerDevice.isSettingsFileVaild(TowerDevices.Grounded)){
                                showError(qsTr("配置文件格式错误"))
                                return false;
                            }
                            if(ControllerManager.towerDevice.connected){
                                enabled  = false
                                ControllerManager.towerDevice.writeSettingsToDevice(0x0001)
                            }
                            else{
                               showWarning(qsTr("请先连接设备"))
                            }
                        }
                        Connections{
                            target:ControllerManager.towerDevice
                            function onWriteSettingsFinished(){
                               set_button.enabled  = true
                               showSuccess(qsTr("设置参数成功"))
                            }
                        }
                    }
                    FluFilledButton{
                        id:query_btn
                        Layout.preferredWidth: 80
                        Layout.alignment: Qt.AlignCenter
                        text:enabled === true? qsTr("查询"): qsTr("查询参数中")
                        onClicked:{
                            if(ControllerManager.towerDevice.connected){
                                enabled  = false
                                ControllerManager.towerDevice.readSettingsFromDevice(0x0001)
                            }
                            else{
                               showWarning(qsTr("请先连接设备"))
                            }
                        }
                        Connections{
                            target:ControllerManager.towerDevice
                            function onReadSettingsFinished(){
                               query_btn.enabled  = true
                               showSuccess(qsTr("查询参数成功"))
                                const dataSource = []
                                table_view.dataSource = dataSource
                               //更新界面//
                               for(var i = 0; i < 4  ;i++){
                                   var group = ControllerManager.towerDevice.getGroundedGroupByIndex(i)
                                   var object = {
                                       id:group.id.valueString,
                                       factor:group.factor.valueString,
                                       temp:group.temp.valueString,
                                       batteryvoltage:group.batteryVoltage.valueString,
                                       apoleresistor: group.aPoleResistor.valueString,
                                       bpoleresistor: group.bPoleResistor.valueString,
                                       cpoleresistor: group.cPoleResistor.valueString,
                                       dpoleresistor: group.dPoleResistor.valueString,
                                       totalresistance:group.totalResistance.valueString,
                                       current:group.current.valueString,
                                       strikescount:group.strikeCount.valueString,
                                       strikestime:group.strikeTime.valueString,
                                       strikescurrent:group.strikeCurrent.valueString,
                                       describeinfo:group.describeInfo.valueString
                                   }
                                   dataSource.push(object)
                                   table_view.dataSource = dataSource
                               }
                            }
                        }
                    }
                    FluFilledButton{
                        Layout.alignment: Qt.AlignCenter
                        text:qsTr("导出")
                        Layout.preferredWidth: 80
                        onClicked: {
                            dialog.open()                        }
                    }
                }
            }
        }
        FluFrame {
            Layout.topMargin: 20
            Layout.fillHeight: true
            Layout.fillWidth: true
            padding: 10
            Item{
                anchors.fill: parent
                FluTableView{
                    id:table_view
                    anchors.fill: parent
                    anchors.topMargin: 5
                    columnSource:[
                        {
                            title: qsTr("ID"),
                            dataIndex: 'id',
                            width:65,
                            minimumWidth:65,
                            maximumWidth:65
                        },
                        {
                            title: qsTr("系数"),
                            dataIndex: 'factor',
                            width:50,
                            minimumWidth:50,
                            maximumWidth:50
                        },
                        {
                            title: qsTr("温度"),
                            dataIndex: 'temp',
                            width:65,
                            minimumWidth:65,
                            maximumWidth:65
                        },
                        {
                            title: qsTr("电池电压"),
                            dataIndex: 'batteryvoltage',
                            width:65,
                            minimumWidth:65,
                            maximumWidth:65
                        },
                        {
                            title: qsTr("A电阻"),
                            dataIndex: 'apoleresistor',
                            width:50,
                            width:50,
                            minimumWidth:50,
                            maximumWidth:50
                        },
                        {
                            title: qsTr("B电阻"),
                            dataIndex: 'bpoleresistor',
                            width:50,
                            minimumWidth:50,
                            maximumWidth:50
                        },
                        {
                            title: qsTr("C电阻"),
                            dataIndex: 'cpoleresistor',
                            width:50,
                            width:50,
                            minimumWidth:50,
                            maximumWidth:50
                        },
                        {
                            title: qsTr("D电阻"),
                            dataIndex: 'dpoleresistor',
                            width:50,
                            minimumWidth:50,
                            maximumWidth:50
                        },
                        {
                            title: qsTr("总电阻值"),
                            dataIndex: 'totalresistance',
                            width:65,
                            minimumWidth:65,
                            maximumWidth:65
                        },
                        {
                            title: qsTr("电流"),
                            dataIndex: 'current',
                            width:50,
                            minimumWidth:50,
                            maximumWidth:50
                        },
                        {
                            title: qsTr("雷击计数"),
                            dataIndex: 'strikescount',
                            width:65,
                            minimumWidth:65,
                            maximumWidth:65
                        },
                        {
                            title: qsTr("雷击时间"),
                            dataIndex: 'strikestime',
                            width:65,
                            minimumWidth:65,
                            maximumWidth:65
                        },
                        {
                            title: qsTr("雷击电流"),
                            dataIndex: 'strikescurrent',
                            width:65,
                            minimumWidth:65,
                            maximumWidth:65
                        },
                        {
                            title: qsTr("描述信息"),
                            dataIndex: 'describeinfo',
                            width:table_view.width - 500
                        }
                    ]
                }
            }
        }
    }
}
